package com.maiji.cloud.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.google.common.collect.Lists;

import net.dongliu.apk.parser.ApkFile;
import net.dongliu.apk.parser.bean.ApkMeta;
import net.dongliu.apk.parser.bean.UseFeature;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class MaijiOSS {

    private static final String ENDPOINT2 = "http://oss-cn-beijing.aliyuncs.com";
    private static final String ENDPOINT = "http://oss-cn-beijing-internal.aliyuncs.com";
    private static final String ACCESSKEYID = "LTAI1SlxSZGC4Etk";
    private static final String ACCESSKEYSECRET = "6kOb747zTRWx7MtqhbeqgqQkbvKhmq";

    public static final String  OSSFOODBUKET= "maiji-food-images";  // 上传食材图片
    public static final String  OSSRECIPEBUKET= "maiji-recipe-images";  // 上传食谱图片
    public static final String  OSSARTICLEBUKET= "maiji-article-images";  // 上传文章图片
    public static final String  OSSGOODSBUKET= "maiji-goods-images";   // 上传商品图片

    public static Boolean aliyunOSSUpload2(String bucketName, String fileName, InputStream inputStream) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(ENDPOINT2, ACCESSKEYID, ACCESSKEYSECRET);
            if (!ossClient.doesBucketExist(bucketName)) ossClient.createBucket(bucketName);
            // 设置存储空间的访问权限为私有。
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            // 上传文件
            ossClient.putObject(bucketName, fileName, inputStream);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally { // 关闭OSSClient
            if (ossClient != null) ossClient.shutdown();
        }
    }

    public static Boolean aliyunOSSUpload(String bucketName, String fileName, InputStream inputStream) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
            if (!ossClient.doesBucketExist(bucketName)) ossClient.createBucket(bucketName);
            // 设置存储空间的访问权限为私有。
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            // 上传文件
            ossClient.putObject(bucketName, fileName, inputStream);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally { // 关闭OSSClient
            if (ossClient != null) ossClient.shutdown();
        }
    }

    public static Boolean aliyunOSSBatchDel2(Map<String, List<String>> map) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(ENDPOINT2, ACCESSKEYID, ACCESSKEYSECRET);
            // 删除文件。
            for (String bucketName: map.keySet()) {
                List<String> objNames = map.get(bucketName);
                DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(objNames);
                ossClient.deleteObjects(deleteObjectsRequest);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally { // 关闭OSSClient
            if (ossClient != null) ossClient.shutdown();
        }
    }

    public static Boolean aliyunOSSBatchDel(Map<String, List<String>> map) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
            // 删除文件。
            for (String bucketName: map.keySet()) {
                List<String> objNames = map.get(bucketName);
                DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName).withKeys(objNames);
                ossClient.deleteObjects(deleteObjectsRequest);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally { // 关闭OSSClient
            if (ossClient != null) ossClient.shutdown();
        }
    }

    public static void main (String[] args) {
//        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET);
//        Calendar calendar = Calendar.getInstance();
//        int year = calendar.get(Calendar.YEAR);
//        int month = calendar.get(Calendar.MONTH);
//
//        String bucketName = "maiji-user-" + year + "-" + month;
//        boolean exists = ossClient.doesBucketExist(bucketName);
//        if (! exists) ossClient.createBucket(bucketName);
//
//        // 上传文件。<yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
//        PutObjectResult maijioss = ossClient.putObject(bucketName, "46597989qwe", new File("D://123.jpg"));
//
//        // 关闭OSSClient。
//        ossClient.shutdown();
        System.out.println(UUID.randomUUID());
    }
    
    /**
     * 获取APK信息
     * @param apkUrl
     * @return
     */
    public static Map<String,Object> readApk(String apkUrl){
    	Map<String,Object> resMap=new HashMap<String,Object>();
    	try (ApkFile apkFile = new ApkFile(new File(apkUrl))) {
                ApkMeta apkMeta = apkFile.getApkMeta();
                resMap.put("versionCode", apkMeta.getVersionCode());
                resMap.put("versionName", apkMeta.getVersionName());      
            } catch (IOException e) {
                e.printStackTrace();
            }
    		
    	return resMap;
    	}

}
